package com.promis.party.preference;

import android.content.Context;

import com.promis.SharedPreferencesHolder;
import com.promis.party.pojo.enums.AccountState;
import com.promis.util.StringUtils;

/**
 * Created by zengyuwen on 2017/9/4.
 *
 */

public class AccountPreferences extends SharedPreferencesHolder {

    private static AccountPreferences preference = null;

    private final static String ACCOUNT = "account";
    private final static String PASSWORD = "password";
    private final static String STATE = "state";

    protected AccountPreferences(Context context) {
        super(context);
    }

    public static synchronized AccountPreferences getInstance(Context context){
        if(preference == null)
            preference = new AccountPreferences(context);
        return preference;
    }

    public void put(String account, String password, int status) {

        editor.putString(ACCOUNT, account);
        editor.putString(PASSWORD, password);
        editor.putInt(STATE, status);

        editor.commit(); // 提交修改
    }

    public void put( String password, String account) {
        if(!StringUtils.isNullOrEmpty(password))
            editor.putString(PASSWORD, password);
        if(!StringUtils.isNullOrEmpty(account))
            editor.putString(PASSWORD, account);
        editor.commit(); // 提交修改
    }

    public boolean exists() {
        return !StringUtils.isNullOrEmpty(getAccount()) && !StringUtils.isNullOrEmpty(getPassword());
    }

    public void remove() {
        editor.remove(ACCOUNT);
        editor.remove(PASSWORD);
        editor.remove(STATE);
        editor.commit();
    }

    public String getAccount() {
        return sharedPreference.getString(ACCOUNT, "");
    }

    public String getPassword() {
        return sharedPreference.getString(PASSWORD, "");
    }

    public int getStatus() {
        return sharedPreference.getInt(STATE, AccountState.NORMAL.getValue());
    }
}
